package com.cuz.binarysearch;


/******
 * 二分查找
 *
 * https://www.bilibili.com/video/BV1d54y1q7k7
 * 讲得太好了
 * 无敌好
 */
public class BinarySearch {

    public static void main(String[] args) {
        //0  1  2  3  4  5
        int[] ints = {1, 2, 3, 4, 5, 5, 8, 9, 10};
        System.out.println(findOneEqual(ints, 11));
        System.out.println(findFirstGE(ints, 4));
        System.out.println(findLastLT(ints, 5));
        System.out.println(findFirstGT(ints, 5));
        System.out.println(findLastGE(ints, 5));
    }


    public static int findOneEqual(int[] arr, int target) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int left = -1;
        int right = arr.length;
        while (left + 1 != right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return -1;
    }

    public static int findFirstGE(int[] arr, int target) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int left = -1;
        int right = arr.length;
        while (left + 1 != right) {
            int mid = (right - left) / 2 + left;
            if (arr[mid] < target) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (arr[right] <= target) {
            return right;
        }
        return -1;
    }

    public static int findLastLT(int[] arr, int target) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int left = -1;
        int right = arr.length;
        while (left + 1 != right) {
            int mid = (right - left) / 2 + left;
            if (arr[mid] < target) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (arr[left] < target) {
            return left;
        }
        return -1;
    }

    public static int findFirstGT(int[]arr,int target){
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int left = -1;
        int right = arr.length;
        while (left + 1 != right) {
            int mid = (right - left) / 2 + left;
            if (arr[mid] <= target) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (arr[right] > target) {
            return right;
        }
        return -1;
    }

    public static int findLastGE(int []arr,int target){
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int left = -1;
        int right = arr.length;
        while (left + 1 != right) {
            int mid = (right - left) / 2 + left;
            if (arr[mid] <= target) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (arr[left] <= target) {
            return left;
        }
        return -1;
    }
}
